package com.cy.server.controller;

import com.cy.server.pojo.Admin;
import com.cy.server.pojo.AdminLoginParam;
import com.cy.server.pojo.RespBean;
import com.cy.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;


/**
 * @peoject: yeb
 * @description: 登录
 * @author: LiShaoZhuo
 * @create: 2021-04-26 16:10
 **/
@Api(tags = "loginController")
@RestController
public class LoginController {

    @Autowired
    IAdminService adminService;

    @ApiOperation("登录之后返回token")
    @PostMapping(value = "/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
    }

    @ApiOperation("获取当前登录的用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (principal == null){
            return null;
        }
        String userName = principal.getName();
        Admin admin = adminService.getAdminByUserName(userName);
        admin.setPassword("******");
        admin.setRoles(adminService.getRolesByAdminId(admin.getId()));
        return admin;
    }
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功！");
    }
}
